﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.IO;
using Newtonsoft.Json;

namespace BasePlatformCommon
{
	[JsonObject(MemberSerialization.OptIn)]
	public class SubControlChain : Device
	{
		public bool HideFlag { get;	set; }						// 界面隐藏标记

		public string MacIndex { get; set; }

		public string MacValue { get; set; }

		[JsonProperty]
		public int DeclareSubControlNum { get; set; }			// 声明的分控数

		[JsonProperty]
		public int TotalSubControlNum { get; set; }				// 总分控数

		[JsonProperty]
		public int ErrorSubControlNum { get; set; }				// 异常分控数

		[JsonProperty]
		public int TotalLampNum { get; set; }					// 总灯点数

		[JsonProperty]
		public int ErrorLampNum { get; set; }					// 异常灯点数

		[JsonProperty]
		public List<SubControl> SubControlList { get; set; }	// 分控数据集合

		public List<Equipment> EquControlList {	get; set; }		// 附属设备数据集合

		// 构造函数
		public SubControlChain()
		{
			deviceType = DeviceType.SubControlChain;

			this.name = "交换机端口";
			if (LanguageOper.available == true)
			{
				this.name = LanguageOper.ReadValue("Chain", "subChain.name");
			}

			HideFlag = false;
			MacIndex = "";
			MacValue = "";

			DeclareSubControlNum = -1;
			TotalSubControlNum = 0;
			TotalLampNum = 0;
			ErrorLampNum = 0;
			ErrorSubControlNum = 0;
			SubControlList = new List<SubControl>();
			EquControlList = new List<Equipment>();
		}

		#region Json交互相关操作

		// 设置分控链路数据
		public void setSubControlChainData(JObject jsonData)
		{
			parseSubControlListData(jsonData);
			
			parseSubControlChainState();
		}

		// 解析分控集合数据
		public void parseSubControlListData(JObject jsonData)
		{
			try
			{
				DeclareSubControlNum = (int)jsonData["DecalreSubControlNum"];
				TotalSubControlNum = (int)jsonData["TotalSubControlNum"];
				for (int subIndex = 0; subIndex < TotalSubControlNum; subIndex++)
				{
					string subNameStr = "SubControl" + subIndex.ToString();
					parseSubControlData((JObject)jsonData[subNameStr]);
				}
			}
			catch
			{
				Log.DebugLogger.Error("SubControlList Parse Error!");
			}
		}

		// 解析分控数据
		private void parseSubControlData(JObject jsonData)
		{
			SubControl subControl = new SubControl();
			subControl.setSubControlData(jsonData);
			SubControlList.Add(subControl);
		}
		
		// 解析分控链路状态
		private void parseSubControlChainState()
		{
			ErrorSubControlNum = 0;
			ErrorLampNum = 0;
			TotalLampNum = 0;
			for (int index = 0; index < TotalSubControlNum; index++)
			{
				if (SubControlList[index].CommState != 0)
				{
					ErrorSubControlNum++;
				}
				TotalLampNum += SubControlList[index].TotalLampNum;
				ErrorLampNum += SubControlList[index].ErrorLampNum;
			}
		}

		#endregion

		// 汇总分控链路统计信息
		public void calcSubChainTotalInfo()
		{
			// 总分控数
			TotalSubControlNum = (SubControlList == null ? 0 : SubControlList.Count);

			// 异常分控数, 总灯具数
			ErrorSubControlNum = 0;
			TotalLampNum = 0;
			foreach (SubControl subControl in SubControlList)
			{
				TotalLampNum += subControl.TotalLampNum;
				ErrorLampNum += subControl.ErrorLampNum;

				if (subControl.hasError || subControl.ErrorLampNum > 0)
				{
					ErrorSubControlNum++;
				}
			}
		}

		// 拼接提示信息
		public string buildTipMessage()
		{
			/*
			calcSubChainTotalInfo();
			StringBuilder sb = new StringBuilder();
			sb.Append(" 分控数" + TotalSubControlNum.ToString());
			sb.Append(",分控异常数" + ErrorSubControlNum.ToString());
			sb.Append(",灯点总数" + TotalLampNum.ToString());
			sb.Append(",灯点异常数" + ErrorLampNum.ToString());
			tipMessage = sb.ToString();
			*/

			tipMessage = formatMac(MacValue);

			return tipMessage;
		}

		// mac地址格式化
		private string formatMac(string mac)
		{
			try
			{
				char[] macCharArray = mac.ToUpper().Trim().ToCharArray();
				
				StringBuilder sb = new StringBuilder();
				sb.Append(macCharArray[0]);
				sb.Append(macCharArray[1]);
				sb.Append("-");
				sb.Append(macCharArray[2]);
				sb.Append(macCharArray[3]);
				sb.Append("-");
				sb.Append(macCharArray[4]);
				sb.Append(macCharArray[5]);
				sb.Append("-");
				sb.Append(macCharArray[6]);
				sb.Append(macCharArray[7]);
				sb.Append("-");
				sb.Append(macCharArray[8]);
				sb.Append(macCharArray[9]);
				sb.Append("-");
				sb.Append(macCharArray[10]);
				sb.Append(macCharArray[11]);

				return sb.ToString();
			}
			catch
			{
				return mac;
			}
		}
	}
}
